﻿using pixeleditor.WorkingSpace.Resource;
using spriteeditor;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pixeleditor
{
    public partial class SpritePicker : Form
    {
        public SpritePicker()
        {
            InitializeComponent();
        }
        public string[] Result;
        EditorApp editorapp;
        public static string[] Dialog(EditorApp app)
        {
            SpritePicker picker = new SpritePicker();
            picker.editorapp = app;
            if (picker.ShowDialog() == DialogResult.OK)
                return picker.Result;
            return null;
        }

        private void SpritePicker_Load(object sender, EventArgs e)
        {
            UpdateFileTree();
        }
        public void UpdateFileTree()
        {
            treeView1.Nodes.Clear();
            var WorkNode = new TreeNode("WorkSpace");


            treeView1.Nodes.Add(WorkNode);

            var path = editorapp.workingSpace.WorkingPath;
            WorkNode.Tag = new Nodetag_Folder() { folder = path };
            FillFileNode(WorkNode, path);

            treeView1.ExpandAll();
        }
        void FillFileNode(TreeNode node, string path)
        {
            System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(path);
            var paths = info.GetDirectories();
            {
                foreach (var dir in paths)
                {
                    var folder = new TreeNode(dir.Name);
                    folder.SelectedImageIndex = folder.ImageIndex = 0;
                    folder.Tag = new Nodetag_Folder() { folder = dir.FullName };
                    node.Nodes.Add(folder);
                    FillFileNode(folder, dir.FullName);
                }
            }
            var files = info.GetFiles();
            foreach (var f in files)
            {
                var name = System.IO.Path.GetFileNameWithoutExtension(f.Name).ToLower();
                if (name == "ttbit.config")
                    continue;
                var ext = System.IO.Path.GetExtension(f.Name).ToLower();
                if (ext == ".meta")
                    continue;
                //是否是有意义的资源
                var filenode = new TreeNode(name);

                bool b = editorapp.workingSpace.GetResourceInfo(name, out string respath, out var meta);
                if (respath != null && respath.ToLower() != f.FullName.ToLower())
                {
                    filenode.BackColor = Color.IndianRed;
                    filenode.Tag = new NodeTag_ErrorFile() { file = respath, message = "错误的文件，可能是重名了，本系统不支持文件重名" };
                    filenode.SelectedImageIndex = filenode.ImageIndex = 2;
                    continue;
                }

                if (meta == null)
                {
                    filenode.BackColor = Color.MediumVioletRed;
                    filenode.Tag = new NodeTag_ErrorFile() { file = respath, message = "错误的文件，无法识别类型" };
                    filenode.SelectedImageIndex = filenode.ImageIndex = 2;
                    continue;
                }
                else
                {
                    filenode.Tag = new NodeTag_Meta() { meta = meta };
                    if (meta.Type != Resource_Sprite.TypeStr)
                        continue;
                }

                filenode.SelectedImageIndex = filenode.ImageIndex = 3;
                node.Nodes.Add(filenode);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Result = new string[listBox1.Items.Count];
            for (var i = 0; i < listBox1.Items.Count; i++)
                this.Result[i] = listBox1.Items[i] as string;
            this.DialogResult = DialogResult.OK;
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            pictureBox1.Image = null;
            if (treeView1.SelectedNode != null)
            {
                if (treeView1.SelectedNode.Tag is NodeTag_Meta meta)
                {
                    pictureBox1.Image = Bitmap.FromFile(meta.meta.srcfile);

                }
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            pictureBox1.Image = null;
            if (listBox1.SelectedItem != null)
            {
                var res = editorapp.workingSpace.GetResourceInfo(listBox1.SelectedItem as string, out var p, out var m);
                pictureBox1.Image = Bitmap.FromFile(m.srcfile);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                if (treeView1.SelectedNode.Tag is NodeTag_Meta meta)
                {
                    if (listBox1.Items.Contains(meta.meta.Name) == false)
                        listBox1.Items.Add(meta.meta.Name);
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0)
            {
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
            }
        }
    }
}
